﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using handvn.Models;

namespace handvn.Controllers
{
    public class HomeController : Controller
    {
       
        private HandvnContext db = new HandvnContext();
        private const int PageSize = 20;

        public ActionResult Index()
        {
            ViewBag.Message = "Welcome to ASP.NET MVC!";

            //return View(db.Category.ToList());
            var queryResults1 = (from p in db.Products.Include("User")
                                orderby p.NgayNhapSanPham descending
                                where (p.TinhTrang == true) 
                                select  p).Take(8) ;
            var queryResults2 = (from p in db.Products.Include("User")
                                 orderby p.SoLuotXem descending
                                 where (p.TinhTrang == true)
                                 select p).Take(8);
            var model = new HomeView
            {
                Product = queryResults1.ToList(),
                ProductView = queryResults2.ToList(),
                //Product = db.Database.SqlQuery<Product>("select top 8 * from Products where TinhTrang='true' ORDER BY  NgayNhapSanPham DESC ").ToList(),
                //ProductView = db.Database.SqlQuery<Product>("select top 8 * from Products where TinhTrang='true' ORDER BY  SoLuotXem DESC ").ToList(),
                Categories = db.Categories.ToList()
            };
            return View(model);
        }

        [HttpGet]
        public ActionResult Search(string keyword,int page=1)
        {
            var queryResults1 = db.Products.Where(p => p.TinhTrang == true && p.TenSanPham.Contains(keyword));
            ViewBag.TotalPages = (int)Math.Ceiling((double)queryResults1.Count() / PageSize);

            var prod = (from p in db.Products.Include("User")
                        orderby p.NgayNhapSanPham descending
                        where (p.TenSanPham.Contains(keyword) && p.TinhTrang == true)
                        select p).Skip((page - 1) * PageSize).Take(PageSize).ToList();

            ViewBag.CurrentPage = page;
            ViewBag.PageSize = PageSize;
            ViewBag.did = keyword;
            ViewBag.OnePageOfProducts = prod;
            //return View(db.Category.ToList());
            //var queryResults1 = var queryResults1 = db.SanPham.Where(p => p.TinhTrang == true && p.TenSanPham.Contains(search));
            
            var model = new HomeView
            {
                Product = prod
                //ProductView = queryResults2.ToList(),
                //Product = db.Database.SqlQuery<Product>("select top 8 * from Products where TinhTrang='true' ORDER BY  NgayNhapSanPham DESC ").ToList(),
                //ProductView = db.Database.SqlQuery<Product>("select top 8 * from Products where TinhTrang='true' ORDER BY  SoLuotXem DESC ").ToList(),
                //Categories = db.Categories.ToList()
            };
            return View(model);
        }

        public ActionResult About()
        {
            return View();
        }
    }
}
